<?PHP

require_once WACT_ROOT . 'db/db.inc.php';
require_once WACT_ROOT .'util/arraydataset.inc.php';
require_once WACT_ROOT .'util/dataspace.inc.php';
require_once APP_ROOT .'model/common/leftmenu.php';
require_once APP_ROOT.'model/common/date.php';
require_once APP_ROOT.'model/common/site.php';

class PageFilter {
    function doFilter(&$vars){

    	/*if(isset($vars['premium'])&&$vars['premium']==1){
    		$vars['prem']='1';
    	}*/

    	$mydate = strtotime($vars['date_added']);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT( $mydate );
    	$dateformatted = gmstrftime('%b %d, %Y',$mydate);
    	$vars['date_added_formatted']=$dateformatted;
    }
}

class Page {

	function Page(){
	}

    function &getTypesList(){
    	$types=array('1'=>'Interview','2'=>'Corporate','0'=>'Other');
    	return $types;
    }

    function getTypeName($id) {
     	$types =& Page::getTypesList();
     	return $types[$id];
    }

    function getPublicURLByID($id) {
        $url = 'pages/details.htm?id='.$id;
    	if (APP_NAME!='.') $url = '/'.APP_NAME.'/'.$url;
    	else  $url = '/'.$url;
    	return $url;
    	//return '/'.APP_NAME.'/'.'pub.page.php?page_id='.$id;
    }

    function getIDByName($name) {
    	$site_id = Site::getSiteID();
    	$id = 0;
     	$rec =& DBC::FindRecord("SELECT page_id
     	from sport_static_pages P where P.headline = ".DBC::makeLiteral($name)."
        and P.site_id = ".DBC::makeLiteral($site_id));
     	if ($rec) return $rec->get('page_id');
     	else {
     		$rec =& DBC::FindRecord("SELECT page_id
     		from sport_static_pages P where P.headline = ".DBC::makeLiteral($name));
     		if ($rec) return $rec->get('page_id');
     		else return 0;
     	}
    }

     function getPublicURLByName($name) {
     		$id = Page::getIDByName($name);
     		$url = Page::getPublicURLByID($id);
     		return $url;
     }



    function delete($id) {
        DBC::execute("DELETE FROM sport_static_pages WHERE page_id=". DBC::makeLiteral($id));
        //DBC::execute("DELETE FROM sport_menu WHERE page_id=". DBC::makeLiteral($id));
    }

    function &getRecord($id) {
        return DBC::FindRecord("SELECT M1.parent_menu_id, M2.title as menu_name, U.*, P.*
        FROM sport_static_pages P
        left outer join sport_users U on P.user_id = U.user_id
        left outer join sport_menu M1 on P.page_id = M1.page_id
        left outer join sport_menu M2 on M1.parent_menu_id = M2.menu_id
        WHERE P.page_id = ". DBC::makeLiteral($id));
    }

    function insertPage(&$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        $page_id = $Record->insertId('sport_static_pages',
            array('user_id', 'type','headline', 'date_added', 'description', 'premium',
            'teaser', 'menu_id','site_id'),
            'page_id');
        // now try to add menu
        $show_menu = $data->get('show_menu');
        $parent_menu_id = $data->get('parent_menu_id');
        $headline = $data->get('headline');
        $url = Page::getPublicURLByID($page_id);
        if ($show_menu && $parent_menu_id) {
        	$ds =& new DataSpace();
        	$ds->set('parent_menu_id', $parent_menu_id);
            $ds->set('page_id', $page_id);
            $ds->set('title', $headline);
            $ds->set('type', 2);
            $ds->set('enable', 1);
            $ds->set('url', $url);

            $menu_id = LeftMenuModel::insertMenu($ds);

            // update page table
            $page_ds =& new DataSpace();
            $page_ds->set('menu_id', $menu_id);
            $Record =& DBC::NewRecord($page_ds);
            $Record->update('sport_static_pages', array('menu_id'), "page_id=". DBC::makeLiteral($page_id));
        }
        return $page_id;

    }

    function updatePage($id, &$data) {
    	$site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        $Record->update('sport_static_pages',
            array('type', 'headline', 'description', 'premium',
            'teaser','site_id'),
            "page_id=". DBC::makeLiteral($id));
    }

    function &getPagesForCombo() {
    	$site_id = Site::getSiteID();
        $sql =
        "  select P.* from sport_static_pages P
        where P.site_id = ".DBC::makeLiteral($site_id)."
        order by P.headline ";
        $rs =& DBC::NewRecordSet($sql);
        $rs->reset();
        $pages = array();
        $pages[0]='(None)';
        while($rs->next()) {
        	$page_id = $rs->get('page_id');
        	$title = $rs->get('headline');
        	$pages[$page_id] = $title;
        }

        return $pages;
    }

    function &getPagesList(&$Pager, &$request, &$responseModel) {
    	$site_id = Site::getSiteID();
        //restore filter from session
     	Session::loadResponseModel($responseModel,'filter.pages');
     	$sql = "select U.*, P.*
     	from sport_static_pages P
     	left outer join sport_users U on P.user_id = U.user_id
      	where 1=1 ".Page::getPagesFilter($request,$responseModel).
      	" and P.site_id = ".DBC::makeLiteral($site_id)." ".
     	" order by ".Page::getPagesOrder($request,$responseModel);
     	//print $sql;
        return DBC::NewPagedRecordSet($sql, $Pager);
     }

     function getPagesFilter(&$request, &$responseModel) {
      	$where = '';
      	$filter_page_id = $responseModel->get('filter_page_id');
        $page_id = $responseModel->get('page_id');
      	if ($filter_page_id)  $where .= ' and P.page_id = '.DBC::makeLiteral($page_id);

        $filter_headline = $responseModel->get('filter_headline');
        $headline = $responseModel->get('headline');
        if ($filter_headline)  $where .= ' and P.headline like '.DBC::makeLiteral('%'.$headline.'%').'  ';

        $filter_author = $responseModel->get('filter_author');
        $author = $responseModel->get('author');
        if ($filter_author)  $where .= ' and P.user_id = '.DBC::makeLiteral($author);

        $filter_type = $responseModel->get('filter_type');
        $type = $responseModel->get('type');
        //print 'Filter: '.$filter_type.$type;
        if ($filter_type)  $where .= ' and P.`type` = '.DBC::makeLiteral($type);

        $filter_start_date = $responseModel->get('start_date_filter');
        $start_day = $responseModel->get('start_day');
        $start_month = $responseModel->get('start_month');
        $start_year = $responseModel->get('start_year');
        $start_date = gmmktime(0, 0, 0, $start_month, $start_day, $start_year);
        $tz =& new MyTimeZone();
        $start_date = $tz->timeLocal( $start_date );
        if ($filter_start_date) $where .= " and P.date_added>='".gmdate("Y.m.d",$start_date)."'";

        $filter_end_date = $responseModel->get('end_date_filter');
        $end_day = $responseModel->get('end_day');
        $end_month = $responseModel->get('end_month');
        $end_year = $responseModel->get('end_year');
        $end_date = gmmktime(0, 0, 0, $end_month, $end_day, $end_year);
        $tz =& new MyTimeZone();
        $end_date = $tz->timeLocal( $end_date );
        if ($filter_end_date) $where .= " and P.date_added<='".gmdate("Y.m.d",$end_date)."'";

        $filter_recent = $responseModel->get('recent_filter');
        $recent = $responseModel->get('recent');
        if ($recent) {
        	$start_date = gmmktime(0, 0, 0, gmdate("m"), gmdate("d")-$recent, gmdate("Y"));
        	$start_date = $tz->timeLocal( $start_date );
        }
        if ($filter_recent) $where .= " and P.date_added>='".date("Y.m.d",$start_date)."'";


        return $where;
      }

      function getPagesOrder(&$request, &$responseModel) {
      	$sortby = 'P.date_added desc';
      	$sortby = $responseModel->get('sort');
      	if ($sortby) return 'P.'.$sortby;
      	else return 'P.date_added desc';
      }


      function getInterviewsList($pager) {
      	$site_id = Site::getSiteID();
      	$sql = "select U.*, P.* from sport_static_pages P ".
     	"left outer join sport_users U on P.user_id = U.user_id ".
      	" where type=1
        and P.site_id = ".DBC::makeLiteral($site_id)."
      	order by date_added desc";
     	error_log("$sql");
        return DBC::NewPagedRecordSet($sql, $pager, new PageFilter());

      }




}

?>
